package com.gdut.simple.view.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gdut.simple.R;
import com.gdut.simple.common.CityUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.view.adapter.AreaAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

public class ProvinceActivity extends AbsBasicActivity {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_area_list)
    RecyclerView mProvinceList;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_area);
        ButterKnife.bind(this);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar.setTitle("选择省份");
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        final List<String> provinces = CityUtils.getProvinces(this);
        if (provinces != null) {
            AreaAdapter adapter = new AreaAdapter(this, provinces);
            mProvinceList.setLayoutManager(new LinearLayoutManager(this));
            mProvinceList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
            mProvinceList.setAdapter(adapter);
            adapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    Intent intent = new Intent(ProvinceActivity.this, CityActivity.class);
                    intent.putExtra("province", provinces.get(position));
                    startActivity(intent);
                }

                @Override
                public boolean onLongClick(View view, int position) {
                    return false;
                }
            });
        }
        RxBus.getDefault().toObservable(UserEvent.class).subscribe(new Action1<UserEvent>() {
            @Override
            public void call(UserEvent userEvent) {
                finish();
            }
        });
    }
}
